package com.cuckoo.cuckoo_app2.ui;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cuckoo.cuckoo_app2.MyApplication;
import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.UserApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.UserApi;
import com.cuckoo.cuckoo_app2.dao.User;
import com.cuckoo.cuckoo_app2.utils.DialogUtils;
import com.google.gson.JsonObject;

public class ChangePasswordAcitvity extends AppCompatActivity {

    private Context context;
    private UserApi userApi;
    private ViewHolder holder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TEST", "MainActivity.onCreate()");
        setContentView(R.layout.activity_change_password);

        this.context = this;
        this.userApi = new UserApiImp();
        this.holder = new ViewHolder();

        this.initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void initView() {

        holder.cp_submint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSubmit();
            }
        });
    }

    /**
     * 提交
     * 校验是否输入密码
     */
    public void onSubmit() {
        User user = MyApplication.instances.getUser();
        if (user == null) {
            return;
        }

        String ordPassword = holder.cp_ord_password.getText().toString();
        String password = holder.cp_password.getText().toString();
        String confirmpassword = holder.cp_new_password.getText().toString();

        if (ordPassword.isEmpty()) {
            DialogUtils.showToast(context, "请输入旧密码");
            return;
        }
        if (password.isEmpty()) {
            DialogUtils.showToast(context, "请输入新密码");
            return;
        }
        if (confirmpassword.isEmpty()) {
            DialogUtils.showToast(context, "请再次输入新密码");
            return;
        }

        if (!password.equals(confirmpassword)) {
            DialogUtils.showToast(context, "两次密码不一致");
            return;
        }

        userApi.performChangePasswordAsync(user.getName(), password, ordPassword, confirmpassword, new BaseCallbac<JsonObject>() {

            @Override
            public void onSuccess(JsonObject data) {
                if (!data.get("success").getAsBoolean()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DialogUtils.showToast(context, data.get("message").getAsString());
                        }
                    });
                    return;
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.showToast(context, data.get("message").getAsString());
                    }
                });

                // 暂停一秒
                DialogUtils.sleep(1000);

                // 返回上一页
                finish();
            }

            @Override
            public void onFailure(String errorMessage) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogUtils.showToast(context, errorMessage);
                    }
                });
            }
        });

    }

    /**
     * 获取相应的ID
     */
    class ViewHolder {

        private EditText cp_ord_password;
        private EditText cp_password;
        private EditText cp_new_password;
        private Button cp_submint;

        public ViewHolder() {
            cp_ord_password = findViewById(R.id.cp_ord_password);
            cp_password = findViewById(R.id.cp_password);
            cp_new_password = findViewById(R.id.cp_new_password);
            cp_submint = findViewById(R.id.cp_submint);
        }
    }
}
